home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
PROGRAMR
/
WINEXIT.ZIP
/
WINEXIT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-19
|
18KB
|
617 lines
/*
* $Id: WINEXIT.C 1.0 1993/07/19 15:26:48 nino Exp $
*
* Copyright (c) 1993. Nino Margetic. All rights reserved.
*
* General permission is hereby granted to copy this
* program freely provided no charge is made for its
* distribution and provided this copyright notice
* remains in place.
*
* Abstract: WINEXIT.C - Exit from Windows from an icon.
*
* This program provides an easy way to get out of Windows.
* It can be run either from the Program Manager or it can
* be started and minimized so that its icon shows up in
* the icon area.
*
* Operating procedures:
*
* This program can be placed in a convenient group in the
* Program Manager or it can be placed in the StartUp group
* so that it shows up in the main icon area. If it is put
* in the StartUp group it should probably be run minimized.
*
* Written: 16-Jul-1993 Nino Margetic <nino@medphys.ucl.ac.uk>
*
* Acknowledgements:
* Thanks to Bruce C. Wright and his EXIT utility which served
* as a template for this exercise in Windows programming.
*
*
*/
#define STRICT
#define WIN31
#include <windows.h>
#include <bwcc.h>
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "winexit.h"
/*
* Main procedure
*/
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
MSG msg;
UINT fuErr;
WORD winVer;
// save the program instance
hInst = hInstance;
// turn off Windows error "file not found" box.
// HOWEVER, it doesn't seem to work for BWCC.DLL ...
fuErr = SetErrorMode ( SEM_NOOPENFILEERRORBOX ) ;
// load the BWCC.DLL
BWCCGetVersion();
// turn it back on.
SetErrorMode ( fuErr ) ;
// check version of Windows. At the moment we support 3.1 or higher!
// this test courtesy of: davidds@microsoft.com (David D'Souza)
winVer = LOWORD(GetVersion());
winVer = (((WORD)(LOBYTE(winVer))) << 8)|(WORD)HIBYTE(winVer);
if (winVer < 0x030A) // NOTE: Always use a HEX value here!!!
{ //exit
char szTemp[256];
LoadString( hInstance,
(UINT)MAKEINTRESOURCE(IDS_BADVERSION),
szTemp, sizeof szTemp );
BWCCMessageBox (NULL, szTemp, szAppName,
MB_ICONSTOP | MB_OK );
FreeLibrary ( hBWCCDLL );
return FALSE;
}
// check command line options
// debug switch.
if ( (lstrcmpi(lpszCmdLine,"/d") == 0 ) ||
(lstrcmpi(lpszCmdLine,"-d") == 0 ) ) iDebugValue = DEBUG;
// check for another instance.
if ( !hPrevInstance ) {
WNDCLASS wndclass;
// first run - register class.
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = ExitWndProc; // our WndProc.
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = DLGWINDOWEXTRA; // IMPORTANT!!!
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_EXIT1));
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = MAKEINTRESOURCE(ExitWindowsMenu);
wndclass.lpszClassName = szClassName;
if (!RegisterClass (&wndclass))
return FALSE;
}
else { // we have another instance running.
HWND hOldWnd;
char szTemp[256];
// if there is an old window, find it and open it up.
if ( NULL != (hOldWnd = FindWindow( szClassName, "Exit Windows")) ) {
// if its iconic, open it up.
if( IsIconic(hOldWnd) ) {
if ( !OpenIcon ( hOldWnd ) ) {
// error. Couldn't open iconized program.
LoadString( hInstance,
(UINT)MAKEINTRESOURCE(IDS_BADOLDICON),
szTemp, sizeof szTemp );
BWCCMessageBox (NULL, szTemp, szAppName,
MB_ICONSTOP | MB_OK );
return FALSE;
}
}
// and bring it to the top.
if ( !BringWindowToTop( hOldWnd ) ) {
// error. Couldn't bring window to top.
LoadString( hInstance,
(UINT)MAKEINTRESOURCE(IDS_BADWINDOWTOP),
szTemp, sizeof szTemp );
BWCCMessageBox (NULL, szTemp, szAppName,
MB_ICONSTOP | MB_OK );
return FALSE;
}
// we found it OK - now bail out.
return FALSE ;
}
else
{ // error. We have a NULL from FindWindow call.
LoadString( hInstance,
(UINT)MAKEINTRESOURCE(IDS_BADOLDWINDOW),
szTemp, sizeof szTemp );
BWCCMessageBox (NULL, szTemp, szAppName,
MB_ICONSTOP | MB_OK );
return FALSE;
}
}
// and tell Windows to that our DialogProc is going to
// handle the dialog
hExitWnd = CreateDialog (hInstance, MAKEINTRESOURCE(ExitDialog),
0, NULL);
// check window handle.
if ( !hExitWnd )
return FALSE;
// show it
ShowWindow (hExitWnd, nCmdShow);
// load accelerators
hAccel = LoadAccelerators ( hInstance, MAKEINTRESOURCE(ExitAccelerators) );
if ( ! hAccel )
return FALSE;
// message loop
while (GetMessage (&msg, NULL, 0, 0))
{
if ( !TranslateAccelerator ( hExitWnd, hAccel, &msg ) )
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
// and exit.
return msg.wParam;
}
// -----------------------------------------------------------------------
/*
* AboutDlgProc - Routine to handle the About dialog window.
*/
#ifdef __BORLANDC__
#pragma argsused
#endif
BOOL FAR PASCAL AboutDlgProc (HWND hDlg, UINT iMessage, UINT wParam, LONG lParam)
{
// set focus to OK button so that the keyboard works.
SetFocus ( GetDlgItem ( hDlg, IDOK ) );
// display credits.
if ((iMessage == WM_COMMAND) && (wParam == IDOK))
EndDialog(hDlg, 0); // dismiss dialog if OK
return(0); // otherwise just sit there
}
// -------------------------------------------------------------------------
/*
* ExitWndProc - Routine to handle main window
*
* All messages that we do NOT process, go to the BWCCDefWindowProc
* which will handle them...
*
*/
LRESULT FAR PASCAL _export ExitWndProc (HWND hDlg, UINT iMessage, UINT wParam, LONG lParam)
{
static HMENU hSysMenu;
FARPROC lpfnAboutDlgProc;
static BOOL fbWriteIni = FALSE ;
static char szMenuExitOption[25];
switch (iMessage)
{
// create window and fix the system menu
case WM_CREATE:
// get the handle to the menu
hSysMenu = GetSystemMenu(hDlg,FALSE);
// add separator line and quick exit.
AppendMenu(hSysMenu, MF_SEPARATOR, 900, NULL);
// default exit mode
iBootAction = IDD_EXIT;
// get default exit mode
if ( IDD_EXIT != ( iBootAction += GetPrivateProfileInt (
szSection, szIconExit,
0, szIniFile)) )
{
// INI file sanity check.
if ( iBootAction < IDD_EXIT ||
iBootAction > IDD_BOOT ) iBootAction = IDD_EXIT;
// we saved the flag (0,1,2) only, *not* the IDD_VALUE.
// iBootAction += IDD_EXIT;
}
// prepare menu strings. The menu item itself will be
// added during the WM_INIEXIT message, when we are sure
// that the dialog has been created already.
switch ( iBootAction )
{
case IDD_EXIT:
wsprintf(szMenuExitOption, "Exit &DOS" );
break;
case IDD_REST:
wsprintf(szMenuExitOption, "Restart &Windows" );
break;
case IDD_BOOT:
wsprintf(szMenuExitOption, "Reboot &System" );
break;
}
// No add the Quick Exit (no questions asked)
AppendMenu(hSysMenu, MF_STRING, IDM_QUICKEXIT, "&Quick Exit");
// check last state, and act accordingly.
if ( EW_SLOW != ( iQuickExitValue = GetPrivateProfileInt(szSection,
szQuickExit, EW_SLOW, szIniFile))) {
// sanity check.
iQuickExitValue = EW_QUICK;
CheckMenuItem(hSysMenu, IDM_QUI